﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Results_Comparer
{
    public class Builder
    {
        // Fields
        private string fileName;

        // Methods
        public Builder(string filename)
        {
            this.fileName = filename;
            this.Load();
        }

        private void Load()
        {
            XmlDocument document = new XmlDocument();
            document.Load(this.fileName);
            XmlNodeList elementsByTagName = document.GetElementsByTagName("Difference");
            foreach (XmlNode node in elementsByTagName)
            {
                this.Process(node);
            }
        }

        private void Process(XmlNode node)
        {
            string type = string.Empty;
            string str2 = string.Empty;
            string str3 = string.Empty;
            string path = string.Empty;
            RdfAttribute item = new RdfAttribute();
            List<RdfAttribute> list = new List<RdfAttribute>();
            XmlNodeList childNodes = node.ChildNodes;
            foreach (XmlNode node2 in childNodes)
            {
                string name = node2.Name;
                if (name == null)
                {
                    goto Label_0319;
                }
                if (!(name == "DifferenceType"))
                {
                    if (name == "InstanceRdfID")
                    {
                        goto Label_00BA;
                    }
                    if (name == "ClassType")
                    {
                        goto Label_00CC;
                    }
                    if (name == "Path")
                    {
                        goto Label_00DE;
                    }
                    if (name == "ChangeItem")
                    {
                        goto Label_0100;
                    }
                    goto Label_0319;
                }
                type = node2.FirstChild.Value;
                continue;
            Label_00BA:
                str2 = node2.FirstChild.Value;
                continue;
            Label_00CC:
                str3 = node2.FirstChild.Value;
                continue;
            Label_00DE:
                if (node2.FirstChild != null)
                {
                    path = node2.FirstChild.Value;
                }
                continue;
            Label_0100:
                if (node2.HasChildNodes)
                {
                    item = new RdfAttribute();
                    foreach (XmlNode node3 in node2.ChildNodes)
                    {
                        switch (node3.Name)
                        {
                            case "Item":
                                if (node3.FirstChild != null)
                                {
                                    item.attributeName = node3.FirstChild.Value;
                                }
                                break;

                            case "ChangeType":
                                if (node3.FirstChild != null)
                                {
                                    item.changeType = node3.FirstChild.Value;
                                }
                                break;

                            case "OldValue":
                                if (node3.FirstChild != null)
                                {
                                    item.attributeOldValue = node3.FirstChild.Value;
                                }
                                break;

                            case "NewValue":
                                if (node3.FirstChild != null)
                                {
                                    item.attributeNewValue = node3.FirstChild.Value;
                                }
                                break;

                            case "OldLinkPath":
                                if (node3.FirstChild != null)
                                {
                                    item.attributeOldPath = node3.FirstChild.Value;
                                }
                                break;

                            case "NewLinkPath":
                                if (node3.FirstChild != null)
                                {
                                    item.attributeNewPath = node3.FirstChild.Value;
                                }
                                break;
                        }
                    }
                    list.Add(item);
                    item = new RdfAttribute();
                }
                continue;
            Label_0319:
                if (node2.Name.Equals(str3) && node2.HasChildNodes)
                {
                    XmlNodeList list3 = node2.ChildNodes;
                    foreach (XmlNode node4 in list3)
                    {
                        if (!node4.Name.Equals("rdf:ID") && !node4.Name.Equals("Type"))
                        {
                            RdfAttribute attribute2 = new RdfAttribute
                            {
                                attributeName = node4.Name
                            };
                            if (type.Equals("A"))
                            {
                                if (node4.FirstChild != null)
                                {
                                    attribute2.attributeNewValue = node4.FirstChild.Value;
                                }
                            }
                            else if (type.Equals("D") && (node4.FirstChild != null))
                            {
                                attribute2.attributeOldValue = node4.FirstChild.Value;
                            }
                            attribute2.changeType = "E";
                            list.Add(attribute2);
                        }
                    }
                }
            }
            RdfId rdfId = new RdfId(str2);
            rdfId.SetPath(path);
            rdfId.rdfType = str3;
            if ((item.attributeName == null) && (list.Count == 0))
            {
                Difference.CreateDifference(type, rdfId, item);
            }
            foreach (RdfAttribute attribute2 in list)
            {
                Difference.CreateDifference(type, rdfId, attribute2);
            }
        }
    }
}